<template>
  <basic-container>
    <XmTable :config="tableConfig" :noWatchParams="noWatchParams" class="table" ref="table">
      <div slot="filter">
        <el-form class="fl" :inline="true" ref="form" :model="noWatchParams">
          <el-form-item label="联系人名称">
            <el-input v-model.trim="noWatchParams.linkName" size="small" placeholder="输入全名"></el-input>
          </el-form-item>
          <el-form-item label="仓库名称">
            <el-input v-model.trim="noWatchParams.warehouseName" size="small" placeholder="仓库名称"></el-input>
          </el-form-item>
          <el-form-item label="服务类型">
            <CategorySelect size="small" v-model="noWatchParams.type" placeholder="服务类型" clearable :whileCodeList="whileCodeList"></CategorySelect>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search" size="small" icon="el-icon-search" style="margin-left: 10px;">搜索
            </el-button>
            <el-button @click="reset" size="small" icon="el-icon-delete" style="margin-left: 10px;">清空</el-button>
          </el-form-item>
        </el-form>
        <div class="fr">
          <el-button icon="el-icon-delete" size="small" @click="patchRemove">批量删除</el-button>
          <el-button icon="el-icon-plus" size="small" type="danger" @click="add">新增</el-button>
        </div>
      </div>

      <template slot="column-warehouseName" slot-scope="{data}">
        <span>{{`${data.warehouseName} - ${data.deptName || ''}`}}</span>
      </template>
      <template slot="column-type" slot-scope="{data}">
        <span>{{addedServicesTypeMap[data.type]}}</span>
      </template>
      <template slot="column-operate" slot-scope="{data}">
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-edit" size="small" @click="edit(data)">
          编辑
        </el-button>
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-document-copy" size="small" @click="copy(data)">
          复制
        </el-button>
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-view" size="small" @click="view(data)">
          查看
        </el-button>
        <el-button class="ml-0 mr-10" type="text" icon="el-icon-delete" size="small" @click="remove(data)">
          删除
        </el-button>
      </template>
    </XmTable>

    <addDialog ref="addDialog" @submit="search"></addDialog>
  </basic-container>
</template>

<script>
import { deleteAddedServices } from "@/api/system/addedServices";
import tableConfig from './config/table'
import addDialog from './addDialog'
import { mapGetters } from "vuex";
const whileCodeList = ["MAGIC", "FLOWER", "GIANT_PANDA", "DELIVERY_MANAGER", "MAKE"]

export default {
  data() {
    return {
      tableConfig,
      whileCodeList,
      noWatchParams: {
        linkName: '',
        warehouseName: '',
        type: '',
      },
    };
  },
  computed: {
    ...mapGetters(['addedServicesTypeList', 'addedServicesTypeMap'])
  },
  methods: {
    search() {
      this.$refs.table.fetch()
    },
    reset() {
      this.noWatchParams = {
        linkName: '',
        warehouseName: '',
        type: '',
      }
      this.$nextTick(() => {
        this.$refs.table.fetch()
      })
    },
    add() {
      this.$refs.addDialog.show({ type: 'add' })
    },
    copy(data) {
      this.$refs.addDialog.show({ type: 'add', data })
    },
    edit(data) {
      this.$refs.addDialog.show({ type: 'edit', data})
    },
    view(data) {
      this.$refs.addDialog.show({ type: 'view', data})
    },
    remove(data) {
      this.$confirm('请确认是否删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAddedServices([data.id]).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.search()
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            });
          }
        })
      })
    },
    getIsEdit(data) {
      return (data.ancestorCode === 'complaint_department' || data.ancestorCode === 'refund_reason') && this.permission.dict_edit
    },
    patchRemove() {
      const selection = this.$refs.table.getSelection()
      if (selection && selection.length) {
        this.$confirm('请确认是否删除所选记录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAddedServices(selection.map(item => item.id)).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.search()
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
          })
        })
      } else {
        this.$message({
          type: 'error',
          message: '请选勾选记录!'
        });
      }
    }
  },
  components: {
    addDialog
  }
};
</script>
<style lang="scss" scoped>

</style>